package com.alipay.pass;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayPassInstanceAddRequest;
import com.alipay.api.request.AlipayPassTemplateAddRequest;
import com.alipay.api.request.AlipayPassTemplateUpdateRequest;
import com.alipay.api.response.AlipayPassInstanceAddResponse;
import com.alipay.api.response.AlipayPassTemplateAddResponse;
import com.alipay.api.response.AlipayPassTemplateUpdateResponse;
import com.alipay.factory.AlipayAPIClientFactory;

/**
 * 卡卷相关业务调用接口
 * 
 * @author zwm
 *
 */
public class AlipayPassManage {

	public static void main(String[] args) throws AlipayApiException {
		publishPass();
		// createPass();
		// queryMenu();
		// modifyMenu();
	}

	/**
	 * 新建模板 appAuthToken如ISV代替商家调用接口，需将商户授权后获取的app_auth_token带上；如商家自己调用，则传null。
	 * bizContent 因该接口业务参数复杂，请详见接口文档
	 */
	public static void createPass() throws AlipayApiException {

		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();

		AlipayPassTemplateAddRequest request = new AlipayPassTemplateAddRequest();
		request.putOtherTextParam("app_auth_token", null);
		String bizContent = "{\"unique_id\":\"134196579750001\"," // 支付宝对该参数的请求有唯一性校验，请保证每次请求该参数唯一
				+ "\"tpl_content\":" + "{\"logo\":\"https://alipass.alipay.com//temps/free/logo.png\","
				+ "\"strip\":\"https://alipass.alipay.com//temps/free/strip.png\","
				+ "\"icon\":\"http://alipassprod.test.alipay.net/temps/free/icon.png\"," + "\"content\":{"
				+ "\"evoucherInfo\":" + "{\"title\":\"风情优惠券\"," + "\"type\":\"coupon\"," + "\"product\":\"free\","
				+ "\"startDate\":\"$validStartDate$\"," + "\"endDate\":\"$validEndDate$\"," + "\"operation\":"
				+ "[{\"format\":\"barcode\",\"message\":\"$code$\",\"messageEncoding\":\"UTF-8\",\"altText\":\"$code$\"}],"
				+ "\"einfo\":{" + "\"logoText\":\"$logoText$\"," + "\"headFields\":"
				+ "[{\"key\":\"status\",\"label\":\"状态\",\"value\":\"$useStateDesc$\",\"type\":\"text\"}],"
				+ "\"primaryFields\":"
				+ "[{\"key\":\"strip\",\"label\":\"\",\"value\":\"$discountInfo$\",\"type\":\"text\"}],"
				+ "\"secondaryFields\":"
				+ "[{\"key\":\"validDate\",\"label\":\"有效期至：\",\"value\":\"$validEndDate$\",\"type\":\"text\"}],"
				+ "\"auxiliaryFields\":[]," + "\"backFields\":"
				+ "[{\"key\":\"description\",\"label\":\"详情描述\",\"value\":\"1.该优惠有效期：截止至2014年06月18日；\n2.凭此券可以享受以下优惠：\n享门市价优惠\n不与其他优惠同享。详询商家。\",\"type\":\"text\"},"
				+ "{\"key\":\"shops\",\"label\":\"可用门店\",\"value\":\"\",\"type\":\"text\"},"
				+ "{\"key\":\"disclaimer\",\"label\":\"负责声明\",\"value\":\"除特殊注明外，本优惠不能与其他优惠同时享受；本优惠最终解释权归商家所有，如有疑问请与商家联系。提示：为了使您得到更好的服务，请在进店时出示本券。\",\"type\":\"text\"}"
				+ "]}," + "\"remindInfo\":{\"offset\":\"2\"}},"
				+ "\"merchant\":{\"mname\":\"hodewu\",\"mtel\":\"\",\"minfo\":\"\"},"
				+ "\"platform\":{\"channelID\":\"$channelID$\",\"webServiceUrl\":\"\"},"
				+ "\"style\":{\"backgroundColor\":\"RGB(255,126,0)\"}," + "\"fileInfo\":"
				+ "{\"formatVersion\":\"2\",\"canShare\":true,\"canBuy\":false,\"canPresent\":true,\"serialNumber\":\"$serialNumber$\",\"supportTaxi\":\"true\",\"taxiSchemaUrl\":\"alipays://platformapi/startapp?appId=20000130&sourceId=20000030&showTitleBar=YES&showToolBar=NO&showLoading=NO&safePayEnabled=YES&readTitle=YES&backBehavior=back&url=/www/index.html\"},"
				+ "\"appInfo\":" + "{\"app\":{},\"label\":\"\",\"message\":\"\"}," + "\"source\":\"alipassprod\","
				+ "\"alipayVerify\":[]" + "}}}";
		request.setBizContent(bizContent);
		AlipayPassTemplateAddResponse response = alipayClient.execute(request);
		System.out.print(response.getBody());// 根据业务处理response
	}

	/**
	 * 发放卡券 appAuthToken如ISV代替商家调用接口，需将商户授权后获取的app_auth_token带上；如商家自己调用，则传null。
	 * bizContent 因该接口业务参数复杂，请详见接口文档
	 */
	public static void publishPass() throws AlipayApiException {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();

		AlipayPassInstanceAddRequest request = new AlipayPassInstanceAddRequest();
		request.putOtherTextParam("app_auth_token", null);
		String bizContent = "{\"recognition_type\":\"1\"," + "\"tpl_id\":\"2017020609535710800488963\"," // tpl_id
																											// 创建时获取
				+ "\"recognition_info\":{\"partner_id\":\"2088702468804908\","
				+ "\"out_trade_no\":\"2088702468804908\"}," + "\"tpl_params\":" + "{"
				+ "\"validStartDate\":\"2016-06-30 20:00:00\"," + "\"validEndDate\":\"2016-12-30 20:00:00\","
				+ "\"channelID\":\"2017012005271493\"," + "\"discountInfo\":\"凭此券即可打8.5折\","
				+ "\"code\":\"20160725762587i\"," + "\"useStateDesc\":\"可使用\"," + "\"logoText\":\"四季风情85折优惠券\","
				+ "\"serialNumber\":\"xm433367366244615532\"}}";
		;
		request.setBizContent(bizContent);
		AlipayPassInstanceAddResponse response = alipayClient.execute(request);
		System.out.print(response.getBody());// 根据业务处理response
	}

	/**
	 * 更新模板 appAuthToken如ISV代替商家调用接口，需将商户授权后获取的app_auth_token带上；如商家自己调用，则传null。
	 * bizContent 因该接口业务参数复杂，请详见接口文档
	 */
	public static void updateTemplate() throws AlipayApiException {
		AlipayClient alipayClient = AlipayAPIClientFactory.getAlipayClient();
		AlipayPassTemplateUpdateRequest request = new AlipayPassTemplateUpdateRequest();
		request.putOtherTextParam("app_auth_token", null);
		String bizContent = "{\"tpl_id\":\"2017020609535710800488963\"," // 支付宝对该参数的请求有唯一性校验，请保证每次请求该参数唯一
				+ "\"tpl_content\":" + "{\"logo\":\"https://alipass.alipay.com//temps/free/logo.png\","
				+ "\"strip\":\"https://alipass.alipay.com//temps/free/strip.png\","
				+ "\"icon\":\"http://alipassprod.test.alipay.net/temps/free/icon.png\"," + "\"content\":"
				+ "{\"evoucherInfo\":"
				+ "{\"title\":\"萧萧沫沫优惠券\",\"type\":\"coupon\",\"product\":\"free\",\"startDate\":\"$validStartDate$\",\"endDate\":\"$validEndDate$\",\"operation\":"
				+ "[{\"format\":\"barcode\",\"message\":\"45612346579465\",\"messageEncoding\":\"UTF-8\",\"altText\":\"45612346579465\"}],"
				+ "\"einfo\":{" + "\"logoText\":\"$logoText$\"," + "\"headFields\":"
				+ "[{\"key\":\"status\",\"label\":\"状态\",\"value\":\"$useStateDesc$\",\"type\":\"text\"}],"
				+ "\"primaryFields\":"
				+ "[{\"key\":\"strip\",\"label\":\"\",\"value\":\"$discountInfo$\",\"type\":\"text\"}],"
				+ "\"secondaryFields\":"
				+ "[{\"key\":\"validDate\",\"label\":\"有效期至：\",\"value\":\"$validEndDate$\",\"type\":\"text\"}],"
				+ "\"auxiliaryFields\":[]," + "\"backFields\":"
				+ "[{\"key\":\"description\",\"label\":\"详情描述\",\"value\":\"1.该优惠有效期：截止至2014年06月18日；\n2.凭此券可以享受以下优惠：\n享门市价优惠\n不与其他优惠同享。详询商家。\",\"type\":\"text\"},"
				+ "{\"key\":\"shops\",\"label\":\"可用门店\",\"value\":\"\",\"type\":\"text\"},"
				+ "{\"key\":\"disclaimer\",\"label\":\"负责声明\",\"value\":\"除特殊注明外，本优惠不能与其他优惠同时享受；本优惠最终解释权归商家所有，如有疑问请与商家联系。提示：为了使您得到更好的服务，请在进店时出示本券。\",\"type\":\"text\"}"
				+ "]}," + "\"remindInfo\":" + "{\"offset\":\"2\"}},"
				+ "\"merchant\":{\"mname\":\"hodewu\",\"mtel\":\"\",\"minfo\":\"\"},"
				+ "\"platform\":{\"channelID\":\"$channelID$\",\"webServiceUrl\":\"\"},"
				+ "\"style\":{\"backgroundColor\":\"RGB(255,126,0)\"},"
				+ "\"fileInfo\":{\"formatVersion\":\"2\",\"canShare\":true,\"canBuy\":false,\"canPresent\":true,\"serialNumber\":\"$serialNumber$\",\"supportTaxi\":\"true\",\"taxiSchemaUrl\":\"alipays://platformapi/startapp?appId=20000130&sourceId=20000030&showTitleBar=YES&showToolBar=NO&showLoading=NO&safePayEnabled=YES&readTitle=YES&backBehavior=back&url=/www/index.html\"},"
				+ "\"appInfo\":{\"app\":{},\"label\":\"\",\"message\":\"\"}," + "\"source\":\"alipassprod\","
				+ "\"alipayVerify\":[]" + "}}}";
		request.setBizContent(bizContent);
		AlipayPassTemplateUpdateResponse response = alipayClient.execute(request);
		System.out.print(response.getBody());// 根据业务处理response
	}
}
